package net.lastcoder.ifserver.database;

import net.lastcoder.ifserver.Globals;
import net.lastcoder.ifserver.database.ConnectionProvider;
import net.lastcoder.ifserver.database.DbConnectionManager;
import net.lastcoder.ifserver.util.IOUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Properties;

/**数据库连接提供
 * auth: WenYF
 * date: 2017/10/21
 */
public class DBHelper {
    private static final Logger Log = LoggerFactory.getLogger(DBHelper.class);

    private static HashMap<String, ConnectionProvider> providers = null;

    public static void reload() {
        deInit();
        init();
    }

    public static void deInit() {
        if (providers != null) {
            for (ConnectionProvider provider : providers.values()) {
                provider.destroy();
            }
        }

        providers = null;
    }

    public static boolean isInit() {
        return providers != null;
    }

    public static void init() {
        loadProperties();
    }

    public static Connection getConnection(String channel) throws SQLException {
       return getConnectionProvider(channel).getConnection();
       // return DbConnectionManager.getConnection();
    }

    private static void loadProperties() {
        providers = new HashMap<>();
        String dir;
        if (!Boolean.getBoolean("local")) {
            dir =  Globals.getHomeDirectory() + File.separator + "res"
                            + File.separator + "fzxy";
        } else {
            dir = "D:\\source\\Ifserver\\src\\res\\fzxy";
        }
        dir = dir + File.separator + "mysql";

        Log.debug("mysql path : " + dir);
        File file = new File(dir);
        File[] files = file.listFiles();

        for (File f : files) {
            if (!f.isDirectory() && f.getName().endsWith("properties")) {
                Properties properties = new Properties();
                InputStream is = null;
                try {
                    is = new FileInputStream(f);
                    properties.load(is);
                    String filename = f.getName();
                    LocalConnectionProvider provider = new LocalConnectionProvider(properties);
                    providers.put(filename.substring(0, filename.indexOf(".")), provider);
                    Log.info("load mysql provider : " + properties.getProperty("name") + " success.");
                } catch (IOException e) {
                    Log.error(e.getMessage(), e);
                } finally {
                    IOUtil.closeSilently(is);
                }
            }
        }

        System.setProperty("org.apache.commons.logging.LogFactory"
                , "net.lastcoder.ifserver.util.CommonsLogFactory");
    }

    private static ConnectionProvider getConnectionProvider(String channel) {
        ConnectionProvider provider = providers.get(channel);
        if (provider == null) {
            provider = providers.get("ios");
        }

        return provider;
    }

}
